package com.zhb.cloud.config;

import java.lang.reflect.Method;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName: RedisConfig
 * @Description: redis 集成配置
 * @author: zhb zhuhongbin@ehomepay.com.cn
 * @date: 2017年10月30日 下午1:27:41
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Autowired
    JedisConnectionFactory jedisConnectionFactory;
    /**
     * @Description: 操作模板
     * @return
     */
    public @Bean RedisTemplate<Object, Object> redisTemplate() {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        return redisTemplate;
    }

    /**
     * RedisTemplate
     * 
     * @return
     */
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> sessionRedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory);
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(sessionRedisSerializer());
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(sessionRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 设置redisTemplate的存储格式（在此与Session没有什么关系）
     * 
     * @return
     */
    @Bean
    public RedisSerializer<Object> sessionRedisSerializer() {
        return new Jackson2JsonRedisSerializer<Object>(Object.class);
    }

    /**
     * 主键生成器
     * 
     * @return
     */
    public @Bean KeyGenerator commonKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                String key = sb.toString();
                return key;
            }
        };
    }

}
